<?php

/**
 * JXHEditor class file.
 *
 * @author jerry2801 <jerry2801@gmail.com>
 * @version alpha 4 (2010-4-2 22:46)
 * @requires xhEditor v1.0.0 rc1 build 100301
 *
 * A typical usage of JXHEditor is as follows:
 * <pre>
 * $this->widget('application.extensions.xheditor.JXHEditor',array(
 *     'model'=>$model,
 *     'attribute'=>'content',
 *     'htmlOptions'=>array('cols'=>80,'rows'=>20,'style'=>'width: 100%; height: 500px;'),
 * ));
 * </pre>
 */

Yii::import('zii.widgets.jui.CJuiInputWidget');

class JXHEditor extends CJuiInputWidget
{
	/**
	 * @var string the locale ID (e.g. 'en', 'zh-cn', 'zh-tw') for the language to be used by the date picker.
	 * If this property is not set, I18N will not be involved. That is, the date picker will show in English.
	 */
	public $language;

    public function run()
    {
		list($name,$id)=$this->resolveNameID();

		if(isset($this->htmlOptions['id']))
			$id=$this->htmlOptions['id'];
		else
			$this->htmlOptions['id']=$id;
		if(isset($this->htmlOptions['name']))
			$name=$this->htmlOptions['name'];
		else
			$this->htmlOptions['name']=$name;

		if($this->hasModel())
			echo CHtml::activeTextArea($this->model,$this->attribute,$this->htmlOptions);
		else
			echo CHtml::textArea($name,$this->value,$this->htmlOptions);

		$options=CJavaScript::encode($this->options);

        $path = dirname(__FILE__).DIRECTORY_SEPARATOR.'source';
        $baseUrl = Yii::app()->getAssetManager()->publish($path);

        $js='jQuery(\'#'.$id.'\').xheditor(true,'.$options.');';

		$cs = Yii::app()->getClientScript();

        $language=isset($this->language)?$this->language:'en';

        $cs->registerScriptFile($baseUrl.'/xheditor-'.$language.'.min.js');

		$cs->registerScript(__CLASS__.'#'.$id, $js);
    }
}